

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>ccqw.geodesy &mdash; CCQW 0.1.1 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/sidebar.js"></script>
    <link rel="top" title="CCQW 0.1.1 documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for ccqw.geodesy</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot; A module to handle Geoids and Datum shifts.</span>

<span class="sd">    :Author: 2009-2011 Roberto Vidmar</span>

<span class="sd">    :Copyright: 2011-2012</span>
<span class="sd">                Nicola Creati &lt;ncreati@inogs.it&gt;</span>
<span class="sd">                Roberto Vidmar &lt;rvidmar@inogs.it&gt;</span>

<span class="sd">    :License: MIT/X11 License (see :download:`license.txt</span>
<span class="sd">                               &lt;../../license.txt&gt;`)</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">interp</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>

<span class="n">_long</span>   <span class="o">=</span> <span class="s">&#39;i&#39;</span>
<span class="n">_double</span> <span class="o">=</span> <span class="s">&#39;d&#39;</span>

<div class="viewcode-block" id="DatumShift"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.DatumShift">[docs]</a><span class="k">class</span> <span class="nc">DatumShift</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; A DatumShift correction class, contains the DatumShift matrix.</span>
<span class="sd">  &quot;&quot;&quot;</span>
<div class="viewcode-block" id="DatumShift.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.DatumShift.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fn</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Load [delta lambda, delta phi] matrix into an array structure.</span>

<span class="sd">        File must be binary with the following structure:</span>

<span class="sd">        * 2 long integers: cols, rows</span>
<span class="sd">        * 4 doubles: xmin, xmax, ymin, ymax (degrees, WGS84)</span>
<span class="sd">        * (cols * rows) doubles: longitude data</span>
<span class="sd">        * (cols * rows) doubles: latitude data</span>

<span class="sd">      :param fn: pathname of file to load</span>
<span class="sd">      :type fn: string</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLon</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLat</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="n">fid</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s">&quot;rb&quot;</span><span class="p">)</span>
    <span class="c"># Load Header</span>
    <span class="n">headerFormat</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
      <span class="n">_long</span><span class="p">,</span> <span class="n">_long</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">)</span>
    <span class="n">header</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">headerFormat</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">=</span> <span class="n">header</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">_double</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">xint</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">yint</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLon</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="n">data</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLat</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">data</span><span class="o">.</span><span class="n">size</span> <span class="o">/</span> <span class="mi">2</span><span class="p">:]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="DatumShift.__repr__"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.DatumShift.__repr__">[docs]</a>  <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;WGS84toRoma40 is a </span><span class="si">%d</span><span class="s"> x </span><span class="si">%d</span><span class="s"> matrix</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Latitude in (</span><span class="si">%.4f</span><span class="s">, </span><span class="si">%.4f</span><span class="s">) degrees</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Longitude in (</span><span class="si">%.4f</span><span class="s">, </span><span class="si">%.4f</span><span class="s">) degrees WGS84</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>
</div>
<div class="viewcode-block" id="DatumShift.datumShift"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.DatumShift.datumShift">[docs]</a>  <span class="k">def</span> <span class="nf">datumShift</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Return datum shift matrix</span>

<span class="sd">      :returns: datumShiftLon, datumShiftLat matrices</span>
<span class="sd">      :rtype: tuple</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLon</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLat</span>
</div>
<div class="viewcode-block" id="DatumShift.getLLCorrection"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.DatumShift.getLLCorrection">[docs]</a>  <span class="k">def</span> <span class="nf">getLLCorrection</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xyzWGS84</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">&#39;interp2d&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Return Delta Lon/Lat correction in degrees for xyzWGS84 dataset.</span>

<span class="sd">        Dataset must be Lon/Lat/Z in degrees WGS84.</span>
<span class="sd">        Return None if at least one point is outside region</span>

<span class="sd">        Available methods:</span>
<span class="sd">          - interp2d (default)</span>
<span class="sd">          - thiessen (faster, no interpolation)</span>

<span class="sd">      :param xyzWGS84: xyz dataset (degrees WGS84)</span>
<span class="sd">      :type fn: numpy array</span>
<span class="sd">      :param method: interpolation method (optional)</span>
<span class="sd">      :type method: string</span>
<span class="sd">      :returns: loncorrection, latcorrection matices</span>
<span class="sd">      :rtype: tuple</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">loncorrection</span> <span class="o">=</span> <span class="n">latcorrection</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="c"># datumShift matrix extends from xmin to xmax</span>
    <span class="c"># and from ymin to ymax. Correction points are</span>
    <span class="c"># computed in the center of the squares.</span>
    <span class="c">#</span>
    <span class="c"># ymax  +-------+-------+--...----+-------+</span>
    <span class="c">#       |       |       |         |       |</span>
    <span class="c">#       |   +   |  +    |         |   +   |  &lt;---- Correction points</span>
    <span class="c">#       |       |       |         |       |</span>
    <span class="c"># ymax- +-------+-------+--...----+-------+</span>
    <span class="c"># yint  |       |       |         |       |</span>
    <span class="c">#       |       |       |         |       |</span>
    <span class="c">#       .       .       .         .       .</span>
    <span class="c">#       .       .       .         .       .</span>
    <span class="c">#       .       .       .         .       .</span>
    <span class="c"># ymin- +-------+-------+--...----+-------+</span>
    <span class="c"># yint  |       |       |         |       |</span>
    <span class="c">#       |   +   |   +   |         |   +   |  &lt;---- Correction points</span>
    <span class="c">#       |       |       |         |       |</span>
    <span class="c"># ymin  +-------+-------+--...----+-------+</span>
    <span class="c">#     xmin   xmin+              xmax-   xmax</span>
    <span class="c">#            xint               xint</span>
    <span class="c">#</span>

    <span class="c"># Search if dataset is inside the region where matrix is defined</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">):</span>
      <span class="c"># Ok, file fits into region, find sqare that contains point:</span>
      <span class="c"># Find sqare that contains point:</span>
      <span class="c">#</span>
      <span class="c"># irow+1 +--------+</span>
      <span class="c">#        |        |</span>
      <span class="c">#        |        |</span>
      <span class="c">#        |        |</span>
      <span class="c"># irow   +--------+</span>
      <span class="c">#       icol    icol+1</span>
      <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s">&#39;thiessen&#39;</span><span class="p">:</span>
        <span class="c"># In this way we use Thiessen (Dirichlet/Voronoi) polygons:</span>
        <span class="c"># &quot;Value at unknown location equals value at nearest known point&quot;</span>
        <span class="n">icol</span> <span class="o">=</span> <span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="n">irow</span> <span class="o">=</span> <span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="n">loncorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLon</span><span class="p">[</span><span class="n">irow</span><span class="p">,</span> <span class="n">icol</span><span class="p">]</span>
        <span class="n">latcorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLat</span><span class="p">[</span><span class="n">irow</span><span class="p">,</span> <span class="n">icol</span><span class="p">]</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">xy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span><span class="o">.</span><span class="n">T</span>
        <span class="n">loncorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="n">interp</span><span class="o">.</span><span class="n">interp2d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLon</span><span class="p">,</span>
          <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">),(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">)),</span> <span class="n">xy</span><span class="p">)</span>
        <span class="n">latcorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="n">interp</span><span class="o">.</span><span class="n">interp2d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_datumShiftLat</span><span class="p">,</span>
          <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">),(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">)),</span> <span class="n">xy</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">loncorrection</span><span class="p">,</span> <span class="n">latcorrection</span>
</div></div>
<div class="viewcode-block" id="Geoid"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.Geoid">[docs]</a><span class="k">class</span> <span class="nc">Geoid</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
  <span class="sd">&quot;&quot;&quot; Geoid correction class, contains the geoid matrix.</span>
<span class="sd">  &quot;&quot;&quot;</span>
<div class="viewcode-block" id="Geoid.__init__"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.Geoid.__init__">[docs]</a>  <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fn</span><span class="p">,</span> <span class="n">gtx</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Load Geoid file into an array structure.</span>

<span class="sd">        - gtx == False (default)</span>
<span class="sd">          File must be binary with the following structure:</span>
<span class="sd">          2 long integers: cols, rows</span>
<span class="sd">          4 doubles: xmin, xmax, ymin, ymax (degrees, WGS84)</span>
<span class="sd">          (cols * rows) doubles: geoid data</span>
<span class="sd">        - gtx == True</span>
<span class="sd">          File must be binary with the following structure:</span>
<span class="sd">          2 long integers: cols, rows</span>
<span class="sd">          4 doubles: xmin, xmax, ymin, ymax (degrees, WGS84)</span>
<span class="sd">          (cols * rows) doubles: geoid data</span>

<span class="sd">      For GTX format: http://vdatum.noaa.gov/dev/gtx_info.html#dev_gtx_binary</span>

<span class="sd">      .. warning:: GTX format is **NOT** tested yet</span>

<span class="sd">      :param fn: pathname of file to load</span>
<span class="sd">      :type fn: string</span>
<span class="sd">      :param gtx: file has gtx format (default is False)</span>
<span class="sd">      :type gtx: bool</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="bp">self</span><span class="o">.</span><span class="n">_geoid</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="n">fid</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fn</span><span class="p">,</span> <span class="s">&quot;rb&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">gtx</span><span class="p">:</span>
      <span class="c"># GTX format: http://vdatum.noaa.gov/dev/gtx_info.html#dev_gtx_binary</span>
      <span class="c"># UNTESTED</span>
      <span class="n">headerFormat</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_long</span><span class="p">,</span> <span class="n">_long</span><span class="p">)</span>
      <span class="c"># Load Header</span>
      <span class="n">header</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">headerFormat</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span> <span class="o">=</span> <span class="n">header</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span>
      <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">_double</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
      <span class="n">headerFormat</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">,</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="n">_long</span><span class="p">,</span> <span class="n">_long</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">,</span> <span class="n">_double</span><span class="p">)</span>
      <span class="c"># Load Header</span>
      <span class="n">header</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">headerFormat</span><span class="p">,</span> <span class="n">count</span><span class="o">=</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">=</span> <span class="n">header</span>
      <span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">fromfile</span><span class="p">(</span><span class="n">fid</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">_double</span><span class="p">)</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">xint</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">yint</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymax</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">rows</span>

    <span class="bp">self</span><span class="o">.</span><span class="n">_geoid</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
</div>
<div class="viewcode-block" id="Geoid.__repr__"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.Geoid.__repr__">[docs]</a>  <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Geoid is a </span><span class="si">%d</span><span class="s"> x </span><span class="si">%d</span><span class="s"> matrix</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rows</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">cols</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Latitude in (</span><span class="si">%.4f</span><span class="s">, </span><span class="si">%.4f</span><span class="s">) degrees</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">)</span>
    <span class="n">s</span> <span class="o">+=</span> <span class="s">&quot;Longitude in (</span><span class="si">%.4f</span><span class="s">, </span><span class="si">%.4f</span><span class="s">) degrees WGS84</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">s</span>
</div>
<div class="viewcode-block" id="Geoid.geoid"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.Geoid.geoid">[docs]</a>  <span class="k">def</span> <span class="nf">geoid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Return the geoid matrix</span>

<span class="sd">      :returns: the geoid matrix</span>
<span class="sd">      :rtype: numpy array</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geoid</span>
</div>
<div class="viewcode-block" id="Geoid.getCorrection"><a class="viewcode-back" href="../../ccqw.html#ccqw.geodesy.Geoid.getCorrection">[docs]</a>  <span class="k">def</span> <span class="nf">getCorrection</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xyzWGS84</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">&#39;interp2d&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; Return Geoid correction for xyzWGS84 dataset.</span>

<span class="sd">        Dataset must be Lon/Lat/Z in degrees WGS84.</span>
<span class="sd">        Correction must be **ADDED** to ellipsoidical height to get</span>
<span class="sd">        geoidical height.</span>
<span class="sd">        Return None if at least one point is outside region</span>

<span class="sd">        Available methods:</span>
<span class="sd">          - interp2d (default)</span>
<span class="sd">          - thiessen (faster, no interpolation)</span>

<span class="sd">      :param xyzWGS84: xyz dataset (degrees WGS84)</span>
<span class="sd">      :type fn: numpy array</span>
<span class="sd">      :param method: interpolation method (optional)</span>
<span class="sd">      :type method: string</span>
<span class="sd">      :returns: geoidcorrection matrix</span>
<span class="sd">      :rtype: numpy array</span>
<span class="sd">      :raises:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">geoidcorrection</span> <span class="o">=</span> <span class="bp">None</span>

    <span class="c"># Search if dataset fits inside the geoid matrix</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span> <span class="ow">and</span>
        <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">):</span>
      <span class="c"># Ok, file fits into region, find sqare that contains point:</span>
      <span class="c">#</span>
      <span class="c"># irow+1 +--------+</span>
      <span class="c">#        |        |</span>
      <span class="c">#        |        |</span>
      <span class="c">#        |        |</span>
      <span class="c"># irow   +--------+</span>
      <span class="c">#       icol    icol+1</span>
      <span class="k">if</span> <span class="n">method</span> <span class="o">==</span> <span class="s">&#39;thiessen&#39;</span><span class="p">:</span>
        <span class="c"># In this way we use Thiessen (Dirichlet/Voronoi) polygons:</span>
        <span class="c"># &quot;Value at unknown location equals value at nearest known point&quot;</span>
        <span class="n">icol</span> <span class="o">=</span> <span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">xint</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="n">irow</span> <span class="o">=</span> <span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">yint</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
        <span class="n">geoidcorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_geoid</span><span class="p">[</span><span class="n">irow</span><span class="p">,</span> <span class="n">icol</span><span class="p">]</span>
      <span class="k">else</span><span class="p">:</span>
        <span class="n">xy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">xyzWGS84</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span><span class="o">.</span><span class="n">T</span>
        <span class="n">geoidcorrection</span> <span class="o">=</span> <span class="o">-</span> <span class="n">interp</span><span class="o">.</span><span class="n">interp2d</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_geoid</span><span class="p">,</span>
          <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">xmin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmax</span><span class="p">),</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ymin</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">ymax</span><span class="p">)),</span> <span class="n">xy</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">geoidcorrection</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/ogs.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">CCQW 0.1.1 documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, Roberto Vidmar, Nicola Creati.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>